import {createRouter, createWebHistory} from 'vue-router'
import Login from "@/views/Login.vue";
import Welcome from "@/views/Welcome.vue";
import PageNotFond from "@/views/error/PageNotFond.vue";
import Register from '@/views/Register.vue'

const routes = [
  {
    path:'/login',
    name:'login',
    component:Login
  },
  {
    path: '/register',
    name: 'register',
    component: Register
  },
  {
    path:'/',
    name:'home',
    component:Welcome,
    children:[]
  },
  {
    path: '/:pathMatch(.*)*',
    meta: {
      title: 'Page Not Found'
    },
    component: PageNotFond,
    hidden:true
  }
]

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes
})

export default router

const loadView = import.meta.glob('../views/**/*.vue')

// 动态处理路由
export const handleNestedRoutes = (routesData)=>{
  router.options.routes[1].children = []
  let asyncRouterArray = []
  routesData.forEach(item=>{
    item.component = loadView[`../${item.component}.vue`]
    asyncRouterArray.push(item)
    router.addRoute('home',item)
  })
  router.options.routes[1].children = [...asyncRouterArray]
}
